<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-08-11 15:06:54
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Table;
use DB;

class TableRepository extends Repositories
    {
    public $relation_models = ["columns", "buttons", 'forms', 'creator', 'updator', 'reports'];

    public function __construct(Table $model)
        {
        $this->model = $model;
        // $this->category = null;
        parent::__construct();
        }

    public function getConfig($rebulder = false)
        {
        $data   = $this->relation()->first();
        $user_m = auth()->user();
        if ($user_m->id != 1) {
            $role_table              = $data->role_table()->whereIn("role_id", $user_m->role->pluck("id"))->get();
            $data['showFooter_auth'] = $role_table->sum("showFooter_auth");
            $data['export_auth']     = $role_table->sum("export_auth");
            $data['setting_auth']    = $role_table->sum("setting_auth");
            }
        else {
            $data['showFooter_auth'] = 1;
            $data['export_auth']     = 1;
            $data['setting_auth']    = 1;
            }
        $data['showFooter_auth'] = 1;
        $data['showFooter']      = $data['showFooter'] && $data['showFooter_auth'] > 0;
        $res                     = [];
        if ($data) {
            $res = $data->toArray();
            if ($rebulder) {
                $res['toolbarConfig'] = $this->setToolbarConfig($data);
                $res['formConfig']    = $this->setFormConfig($data);
                $res['pagerConfig']   = $this->setPagerConfig($data);
                $res['treeConfig']    = $this->setTreeConfig($data);
                $res['columns']       = $this->setColumns($data);
                }
            }
        return $res;
        }
    private function setColumns($data)
        {
        $user  = auth()->user();
        $colum = $user->role->pluck("column");
        if ($colum->count() > 0) {
            $columns_ids = $user->role->pluck("column")[0]?->pluck("id")->toArray();
            }
        else
            $columns_ids = [];
        return $data->columns = $data->columns->filter(function ($v) use ($user, $columns_ids)
            {
            if ($v->fixed == '' || is_numeric($v->fixed)) {
                $v->fixed = false;
                }
            if ($v->auth && $user->id != "1" && !in_array($v->id, $columns_ids)) {
                return false;
                }
            return true;
            })->values();
        }
    private function setFormConfig($data)
        {

        $formConfig['titleWidth'] = $data['titleWidth'];
        $formConfig['titleAlign'] = $data['titleAlign'];

        $formConfig['items'] = $this->setFormItem($data['forms']);
        unset($data['forms'], $data['titleAlign'], $data['titleWidth']);
        return $formConfig;
        }
    private function setTreeConfig($data)
        {
        $treeConfig = null;
        if ($data['tree']) {
            $treeConfig = [
                // 'transform'   => true,
                'expandAll'   => true,
                'rowField'    => 'id',
                'parentField' => 'parent_id',
                'iconOpen'    => 'vxe-icon-folder-open',
                'iconClose'   => 'vxe-icon-folder',
                'accordion'   => false,
                'expandField' => $data->treefield,
            ];
            }
        return $treeConfig;
        // :tree-config="{transform: true, expandAll: true}">
        }
    private function setFormItem($forms)
        {

        // itemRender
        $forms->filter(function ($v, $k) use (&$forms)
            {
            // { value: "autocomplet", label: "自动" },
            // { value: "autochoose", label: "弹窗" },
            // { value: "input", label: "输入" },
            // { value: "select", label: "选择" },
            // { value: "checkbox", label: "多选" },
            // $itemRender['name'] = "$" . $v->editfilter;
            $itemRender['name']         = "formRender";
            $itemRender['field']        = $v->field;
            $itemRender['editfilter']   = in_array($v->editfilter, ['autocomplet', 'autochoose', 'select', 'checkbox']) ? $v->editfilter : $v->type;
            $itemRender['editkey']      = $v->editkey;
            $itemRender['placeholder']  = $v->placeholder;
            $itemRender['defaultvalue'] = $v->defaultvalue;
            unset($v['editfilter'], $v['placeholder']);
            $forms[$k]['itemRender'] = $itemRender;
            // $v['itemRender'] = 
            });

        count($forms) > 0 && $forms[] = ["slots" => ['default' => 'btns']];

        return $forms;
        }
    private function setPagerConfig(&$data)
        {
        $pagerConfig               = [];
        $pagerConfig['autoHidden'] = true;
        if ($data['tree'] == false) {
            $pagerConfig['pageSize']  = $data['pageSize'];
            $pagerConfig['pageSizes'] = array_map(function ($v)
                {
                return (int) $v;
                }, explode(",", $data['pageSizes']));
            unset($data['pageSize'], $data['pageSizes']);

            $pagerConfig['enabled'] = true;
            }
        else {

            $pagerConfig['pageSize'] = -1;
            }
        return $pagerConfig;
        }
    private function setToolbarConfig(&$data)
        {
        foreach ($data['buttons'] as $v) {
            if ($v['isprint']) {
                // $v['children'] = [];

                if (!isEmpty($data['reports'])) {

                    foreach ($data['reports'] as $print) {

                        $print['icon'] = "el-icon-back";

                        $print['color']    = "#00CED1";
                        $print['model_id'] = $v['model_id'];
                        $print['url']      = $print->getUrlAttribute();
                        $print['code']     = "printReport";
                        $v['children'][]   = $print;
                        }
                    }
                else
                    continue;
                }

            $toolbarConfig['buttons'][] = [
                'buttonRender' => [
                    'name'   => "LeftButton",
                    'button' => $v,
                    'events' => '{ click: gridEvents.toolbarButtonClick }'
                ]
            ];
            }

        $toolbarConfig['import']  = false;
        $toolbarConfig['export']  = $data['export'] && $data['export_auth'] > 0;
        $toolbarConfig['setting'] = $data['setting'] && $data['setting_auth'] > 0;
        $toolbarConfig['print']   = $data['print'];

        $toolbarConfig['refresh'] = $data['refresh'];
        $toolbarConfig['search']  = $data['search'];

        $toolbarConfig['reload'] = $data['reload'];
        $toolbarConfig['zoom']   = $data['zoom'];
        $toolbarConfig['custom'] = $data['custom'];

        unset(
            $data['buttons'],
            $data['refresh'],
            $data['search'],
            $data['import'],
            $data['export'],
            $data['zoom'],
            $data['custom'],
            $data['setting']
        );

        return $toolbarConfig;
        }
    private function setEditRender(&$data)
        {
        // itemRender
        if ($data['editable']) {
            $data['editRender'] = ['name' => $data['editfilter']];
            $data['editRender'] = ['name' => $data['editfilter']];
            }
        }
    }